﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;
using Microsoft.VisualStudio.Tools.Applications.Runtime;
using Excel = Microsoft.Office.Interop.Excel;
using Office = Microsoft.Office.Core;

namespace ETRoboConResultEditorExcel
{
    public partial class Sheet7
    {
        private static readonly int POS_TeamIdCol = 1;
        private static readonly int POS_TeamBibCol = 2;
        private static readonly int POS_TeamNameCol = 3;
        private static readonly int POS_ModelPointCol = 5;
        private static readonly int POS_ResultPointCol = 4;
        private static readonly int POS_StatusCol = 6;
        private static readonly int POS_EntryRow = 1;
        private static readonly int POS_WinMinRankCol = 10;
        private static readonly int POS_BorderPointCol = 12;
        private static readonly int POS_HarmonizedAveCol = 7;

        private static readonly int POS_TeamCountCol = 8;
        private static readonly int POS_TeamCountRow = 1;
        private static readonly int POS_CtrlCellsRow = 1;

        private void Sheet7_Startup(object sender, System.EventArgs e)
        {
        }

        private void Sheet7_Shutdown(object sender, System.EventArgs e)
        {
        }

        public void UpdateResultInfo(ETRoboConRM.TeamTotalResult[] results)
        {
            string winMinRankStr = ((Excel.Range)Globals.Sheet7.Cells[POS_CtrlCellsRow, POS_WinMinRankCol]).Value2.ToString();
            int winMinRank = int.Parse(winMinRankStr);

            ((Excel.Range)Globals.Sheet7.Cells[POS_TeamCountRow, POS_TeamCountCol]).Value2 = results.Length;
            for (int i = 0; i < results.Count(); i++)
            {
                ((Excel.Range)Globals.Sheet7.Cells[POS_EntryRow + i + 1, POS_TeamIdCol]).Value2 = results[i].TeamId;
                ((Excel.Range)Globals.Sheet7.Cells[POS_EntryRow + i + 1, POS_TeamBibCol]).Value2 = results[i].Bib;
                ((Excel.Range)Globals.Sheet7.Cells[POS_EntryRow + i + 1, POS_TeamNameCol]).Value2 = results[i].Name;
                ((Excel.Range)Globals.Sheet7.Cells[POS_EntryRow + i + 1, POS_ResultPointCol]).Value2 = results[i].NormalizedResultPoints;
                ((Excel.Range)Globals.Sheet7.Cells[POS_EntryRow + i + 1, POS_ModelPointCol]).Value2 = results[i].NormalizedModelPoint;
                string status = "";
                if (results[i].InCourseResult.CompareTo("COMPLETED") == 0)
                {
                    if (results[i].OutCourseResult.CompareTo("COMPLETED") == 0)
                    {
                        status = "○○";
                    }
                    else
                    {
                        status = "○×";
                    }
                }
                else
                {
                    if (results[i].OutCourseResult.CompareTo("COMPLETED") == 0)
                    {
                        status = "×○";
                    }
                    else
                    {
                        status = "××";
                    }
                }
                ((Excel.Range)Globals.Sheet7.Cells[POS_EntryRow + i + 1, POS_StatusCol]).Value2 = status;
                if (results[i].NormalizedModelPoint != null && results[i].NormalizedResultPoints != null)
                {
                    double normRP = results[i].NormalizedResultPoints.Value;
                    double normMP = results[i].NormalizedModelPoint.Value;
                    double harmonized = 2 * normMP * normRP / (normMP + normRP);
                    ((Excel.Range)Globals.Sheet7.Cells[POS_EntryRow + i + 1, POS_HarmonizedAveCol]).Value2 = harmonized;
                    if (winMinRank == results[i].Rank)
                    {
                        ((Excel.Range)Globals.Sheet7.Cells[POS_CtrlCellsRow, POS_BorderPointCol]).Value2 = harmonized;
                    }
                }
            }
        }

        #region VSTO Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(Sheet7_Startup);
            this.Shutdown += new System.EventHandler(Sheet7_Shutdown);
        }

        #endregion

    }
}
